import configparser
import os


class FileIni:
    config = configparser.ConfigParser()
    flag = True
    file_ini_path = './datafiles/config.ini'
    file_ini_dir = './datafiles'

    @staticmethod
    def init():
        # Logger.logger.info('ini文件初始化。。。')
        try:
            if not os.path.exists(FileIni.file_ini_dir):
                os.mkdir(FileIni.file_ini_dir)
                FileIni.flag = False
            if not os.path.exists(FileIni.file_ini_path):
                with open(FileIni.file_ini_path, 'w'):  # 创建文件
                    pass
                # Path(file_ini_dir).touch()
                FileIni.flag = False
            # 创建文件初始内容
            if not FileIni.flag:
                FileIni.config.add_section('count')
                # FileIni.config.set('count', 'rate', '0')
                FileIni.config.set('count', 'total', '0')
                FileIni.config.set('count', 'pass', '0')
                # FileIni.config.set('count', 'fail', '0')
                # 预期版本配置
                # FileIni.config.add_section('config')
                # FileIni.config.set('config', 'version', 'v2.24')
                # SN长度配置
                FileIni.config.add_section('sn')
                FileIni.config.set('sn', 'length', '5')
                with open(FileIni.file_ini_path, 'w', encoding='utf-8') as file:
                    FileIni.config.write(file)
            # Logger.logger.info('ini文件初始化成功')
        except Exception as e:
            # Logger.logger.error('ini文件初始化失败')
            pass

    @staticmethod
    def read_test_count():
        # 读取数据
        # Logger.logger.info('准备读取ini文件数据————测试次数')
        FileIni.config.read(FileIni.file_ini_path, encoding='utf8')
        # Logger.logger.info('正在读取ini文件 打开成功')
        # rate = FileIni.config.get('count', 'rate')
        total_count = FileIni.config.get('count', 'total')
        pass_count = FileIni.config.get('count', 'pass')
        # fail_count = FileIni.config.get('count', 'fail')
        # Logger.logger.info('ini文件数据读取成功————测试次数')
        return total_count, pass_count

    @staticmethod
    def write_test_count(total_count, pass_count):
        # FileIni.config.set('count', 'rate', rate)
        FileIni.config.set('count', 'total', str(total_count))
        FileIni.config.set('count', 'pass', str(pass_count))
        # FileIni.config.set('count', 'fail', str(fail_count))

        with open(FileIni.file_ini_path, 'w') as f:
            FileIni.config.write(f)

    # @staticmethod
    # def read_version():
    #     # Logger.logger.info('准备读取ini文件数据————版本')
    #     FileIni.config.read(FileIni.file_ini_path, encoding='utf8')
    #     # Logger.logger.info('正在读取ini文件 打开成功')
    #     version = FileIni.config.get('config', 'version')
    #     # Logger.logger.info('ini文件数据读取成功————版本')
    #     print('version:', version)
    #     return version
    #
    # @staticmethod
    # def write_version(version: str):
    #     FileIni.config.set('config', 'version', version)
    #     with open(FileIni.file_ini_path, 'w') as f:
    #         FileIni.config.write(f)

    @staticmethod
    def read_sn_length():
        # Logger.logger.info('准备读取ini文件数据————SN长度')
        FileIni.config.read(FileIni.file_ini_path, encoding='utf8')
        # Logger.logger.info('正在读取ini文件 打开成功')
        sn_length = FileIni.config.get('sn', 'length')
        # Logger.logger.info('ini文件数据读取成功————版本')
        print('version:', sn_length)
        return sn_length

    @staticmethod
    def write_sn_length(sn_length: int):
        FileIni.config.set('sn', 'length', str(sn_length))
        with open(FileIni.file_ini_path, 'w') as f:
            FileIni.config.write(f)